﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using X.Entity;
using X.Web;

namespace X.WebSite.Admin
{
    public partial class Login : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //TODO:验证

            SHA1 sha1 = SHA1.Create();
            byte[] password = sha1.ComputeHash(Encoding.Unicode.GetBytes(txtPass.Value.Trim()));

            UserInfo userInfo = Bll.User.AuthenticateUser(txtUsername.Value.Trim(), password);

            if (userInfo.IsNull)
            {
                //用户不存在
            }
            else
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userInfo.LoginName, DateTime.Now, DateTime.Now.AddMinutes(30), true, userInfo.LoginName.GetHashCode().ToString(), FormsAuthentication.FormsCookiePath);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                if (ticket.IsPersistent)
                    cookie.Expires = ticket.Expiration;

                Response.Cookies.Add(cookie);

                Response.Redirect("Default.aspx");
            }
        }
    }
}